package cn.smbms.controller.bill;

import cn.smbms.pojo.Bill;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.bill.BillService;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.tools.Constants;
import cn.smbms.tools.PageSupport;
import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/jsp/bill")
public class BillController {


	@Autowired
	BillService billService;
	@Autowired
	ProviderService providerService;

	@RequestMapping("/getproviderlist")
	@ResponseBody
	private String getProviderlist(){
		//把providerList转换成json对象输出
		return JSONArray.toJSONString(providerService.getProviderList("","",1,providerService.getcount("","")));
	}


	@RequestMapping("/{url}/{billid}")
	private String  getBillById(@PathVariable("url") String url,@PathVariable("billid") String billid,Model model){
		if(!StringUtils.isNullOrEmpty(billid)){
			Bill bill = null;
			bill = billService.getBillById(billid);
			model.addAttribute("bill", bill);
			return url;
		}else{
			return "error";
		}
	}

	@RequestMapping("/modifysave")
	private String  modify(@ModelAttribute Bill bill, HttpSession session)  {
		System.out.println(bill.getId());
		bill.setModifyBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
		bill.setModifyDate(new Date());
		boolean flag = false;
		flag = billService.modify(bill);
		if(flag){
			return "redirect:/jsp/bill/query";
		}else{
			return "billmodify";
		}
	}

	@RequestMapping("/delbill")
	@ResponseBody
	private String  delBill(String billid) {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(!StringUtils.isNullOrEmpty(billid)){
			boolean flag = billService.deleteBillById(billid);
			if(flag){//删除成功
				resultMap.put("delResult", "true");
			}else{//删除失败
				resultMap.put("delResult", "false");
			}
		}else{
			resultMap.put("delResult", "notexit");
		}
		//把resultMap转换成json对象输出
		return JSONArray.toJSONString(resultMap);
	}
	@RequestMapping("/add")
	private String  add(@ModelAttribute Bill bill,HttpSession session) {
		bill.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
		bill.setCreationDate(new Date());
		boolean flag = false;
		flag = billService.add(bill);
		System.out.println("add flag -- > " + flag);
		if(flag){
			return "redirect:/jsp/bill/query";

		}else{
			return "billadd";
		}
	}
	@RequestMapping("/query")
	private String  query(Model model,String queryProductName,String queryProviderId,String queryIsPayment,@RequestParam(value = "pageIndex",defaultValue = "0")String pageIndex) {
		
		List<Provider> providerList = new ArrayList<Provider>();


		//设置页面容量
		int pageSize = Constants.pageSize;
		//当前页码
		int currentPageNo = 1;

		if(pageIndex != null){
			try{
				currentPageNo = Integer.valueOf(pageIndex);
			}catch(NumberFormatException e){
				return "error" ;
			}
		}
		//总数量（表）
		int totalCount	= billService.getCount(queryProductName,queryProviderId,queryIsPayment);
		//总页数
		PageSupport pages=new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		//控制首页和尾页
		if(currentPageNo < 1){
			currentPageNo = 1;
		}else if(currentPageNo > totalPageCount){
			currentPageNo = totalPageCount;
		}
		providerList = providerService.getProviderList("","",1,providerService.getcount("",""));
		model.addAttribute("providerList", providerList);


		if(StringUtils.isNullOrEmpty(queryProductName)){
			queryProductName = "";
		}
		
		List<Bill> billList = new ArrayList<Bill>();
		
		Bill bill = new Bill();

		if(StringUtils.isNullOrEmpty(queryIsPayment)){
			bill.setIsPayment(0);
		}else{
			bill.setIsPayment(Integer.parseInt(queryIsPayment));
		}
		
		if(StringUtils.isNullOrEmpty(queryProviderId)){
			bill.setProviderId(0);
		}else{
			bill.setProviderId(Integer.parseInt(queryProviderId));
		}
		bill.setProductName(queryProductName);
		System.out.println(bill.getProductCount());

		billList = billService.getBillList(bill,currentPageNo, pageSize);
		model.addAttribute("billList", billList);
		model.addAttribute("queryProductName", queryProductName);
		model.addAttribute("queryProviderId", queryProviderId);
		model.addAttribute("queryIsPayment", queryIsPayment);

		model.addAttribute("totalPageCount",totalPageCount);
		model.addAttribute("totalCount",totalCount);
		model.addAttribute("currentPageNo",currentPageNo);
		return "billlist";

	}



}
